/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servletuser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessionbean.AccountFacadeLocal;
import sessionbean.SemesterFacadeLocal;
import sessionbean.StaffProfileFacadeLocal;
import sessionbean.deleteuserLocal;
import supportlearning.entities.Account;
import supportlearning.entities.Semester;
import supportlearning.entities.StaffProfile;

/**
 *
 * @author Huy
 */
public class manageStaff extends HttpServlet {

    @EJB
    private SemesterFacadeLocal semesterFacade;
    @EJB
    private StaffProfileFacadeLocal staffProfileFacade;
    @EJB
    private deleteuserLocal deleteuser;
    @EJB
    private AccountFacadeLocal accountFacade;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean rs = false;
            String action = request.getParameter("action");
            if (action == null) {
                List<Account> lisacc = accountFacade.findbyRoleID(2);
                List<Semester> listse = semesterFacade.findAll();
                request.setAttribute("listacc", lisacc);
                request.setAttribute("listsem", listse);
                request.getRequestDispatcher("managestaff.jsp").forward(request, response);
            } //               else if (action.equals("DeleteSelect")) {
            //                String[] id = request.getParameterValues("cbDelete");
            //                if (id.length != 0) {
            //
            //                    for (int i = 0; i < id.length; i++) {
            //                        rs = deleteuser.deleteuser(id[i].toString());
            //                        System.out.println("ID Selected: " + id[i]);
            //                        response.sendRedirect("manageStaff");
            //
            //                    }
            //                } else {
            //                    out.println("<H1>Khong lay dc ID</H1>");
            //                }
            //            }
            else if (action.equals("linkid")) {
                String id = request.getParameter("id");
                Account acc = accountFacade.find(id);

                request.setAttribute("accstaff", acc);
                request.getRequestDispatcher("updateStaff.jsp").forward(request, response);
            } else if (action.equals("Update")) //create user
            {
                String id = request.getParameter("staffid");
                String firstname = request.getParameter("txtfirstname");
                String lastname = request.getParameter("txtlastname");

                String password = request.getParameter("txtpassword");

                String address = request.getParameter("txtaddress");
                String email = request.getParameter("txtemail");
                boolean gender = Boolean.parseBoolean(request.getParameter("drgender"));

                String des = request.getParameter("desstaff");

                int semid = Integer.parseInt(request.getParameter("semeterid"));
                Account accobj = accountFacade.find(id);
                if (accobj != null) {
                    accobj.setFirstName(firstname);
                    accobj.setLastName(lastname);
                    accobj.setPassword(password);
                    accobj.setAddress(address);
                    accobj.setEmail(email);
                    accobj.setGender(gender);
                    accobj.getStaffProfile().setDescription(des);
                    accobj.getStaffProfile().setSemesterId(semid);
                    accountFacade.edit(accobj);
                    response.sendRedirect("updateStaffSuccess.jsp");
                } else {
                    out.print("User ko ton tai.");
                }

            } else if (action.equals("Sort")) {

                int semid = Integer.parseInt(request.getParameter("selectsortbysem"));
                List<Account> lisacc = new ArrayList<Account>();

                List<StaffProfile> liststaffpro = staffProfileFacade.findbysemid(semid);
                if (liststaffpro.size() != 0) {
                    for (int i = 0; i < liststaffpro.size(); i++) {
                        Account acc = accountFacade.find(liststaffpro.get(i).getStaffId());
                        if (acc != null) {
                            lisacc.add(acc);
                        }

                    }
                    List<Semester> listse = semesterFacade.findAll();
                    request.setAttribute("listacc", lisacc);
                    request.setAttribute("listsem", listse);
                    request.getRequestDispatcher("managestaff.jsp").forward(request, response);
                } else {
                    out.print("Semeter nay ko co ai day.");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
